import 'dart:io';

import 'package:emby/model/media.dart';
import 'package:emby/pages/video/media_controller.dart';
import 'package:emby/util/function.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';

class VideoPlayer extends StatefulWidget {
  final Media media;
  VideoPlayer(this.media);
  @override
  _VideoPlayerState createState() => _VideoPlayerState();
}

class _VideoPlayerState extends State<VideoPlayer> {
  IjkMediaController controller = IjkMediaController();
  bool showController = false;
  @override
  void initState() {
//    SystemChrome.setPreferredOrientations([
//      DeviceOrientation.landscapeLeft,
//    ]);
//    SystemChrome.setEnabledSystemUIOverlays([]);
    var headers = {
      "X-Emby-Authorization": 'Emby UserId="${Util.userId}", Client="${Platform.isIOS ? "iOS" : "Android"}", Device="Emby Player", DeviceId="Emby Player", Version="1.0.0.0"',
      "X-Emby-Token": Util.token
    };
    print("${Util.baseUrl}Videos/${widget.media.id}/stream.${widget.media.container}");
    controller.setNetworkDataSource('${Util.baseUrl}Videos/${widget.media.id}/stream.${widget.media.container}', headers: headers, autoPlay: true);
    super.initState();
  }

  @override
  void dispose() {
//    SystemChrome.setPreferredOrientations([
//      DeviceOrientation.portraitUp,
//    ]);
//    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        // width: MediaQuery.of(context).size.width,
        // height: 400,
        child: Container(
          // height: 400, // 这里随意
          child: IjkPlayer(
            mediaController: controller,
            controllerWidgetBuilder: (IjkMediaController mediaController) {
              return DefaultIJKControllerWidget(
                controller: mediaController,
//                currentFullScreenState: true,
                fullScreenType: FullScreenType.rotateScreen,
                volumeType: VolumeType.system,
                hideSystemBarOnFullScreen: true,
                fullscreenControllerWidgetBuilder: (IjkMediaController mediaController) {
                  return IJKControllerWidget(
                    controller: mediaController,
                    media: widget.media,
                  );
                },
              );
            },
          ),
        ),
      ),
    );
  }
}
